package wiiusej.apptest.generalTasks;


import javax.sound.*;


import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.Callable;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;

import wiiusej.Wiimote;
import wiiusej.values.RawAcceleration;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;

public class OnMotionSensingTask implements Callable {

	private Wiimote myWiimote;
	private boolean buttonState;
	private MotionSensingEvent event;
	
	//Logger URL
	private static final String fileURL = "src/test/java/wiiusej/resources/logs/tasks/Acc_Wiimote_";
	
	//Date format
	public static final String DATE_FORMAT = "HH:mm:ss";
	
	static Logger fileLogger;
	
	private static Timestamp ts;

	
	
	public OnMotionSensingTask(Wiimote wiimote,	MotionSensingEvent incomingEvent, 
			boolean buttonState){
		
		this.buttonState = buttonState;
		this.event = incomingEvent;
		this.myWiimote = wiimote;
		
		PropertyConfigurator.configure("log4j.properties");
		this.fileLogger = Logger.getLogger("motionSensor");
		
		PatternLayout pattern = 
			new PatternLayout("%r %-5p %c %x - %m%n");		
		
		try {			
			//Creating a file for every connected wiimote
			FileAppender fileappend=new FileAppender(pattern,fileURL + wiimote.getId() + ".log");
			fileLogger.addAppender(fileappend);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	
	@Override
	public Integer call() throws Exception {		

		//Get the Acceleration vector
		RawAcceleration acc = new RawAcceleration();
		acc = event.getRawAcceleration();
		
		if(buttonState){			
			//Write into the corresponding file
			fileLogger.info(event);			
		}
		
		if(acc.getX() > 150){
			return 1;				
		}else{
			return 0;
		}
		
		
		
	}

}
